<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in myChannels"
        :key="index"
        @click="onMyChannelClick(item, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fiexdChannels.includes(item.id)"
        ></van-icon>
        <span class="text" slot="text" :class="{ active: index === active }">{{
          item.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recomment-grid" :gutter="10">
      <van-grid-item
        icon="plus"
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>
  
  <script>
import { getAllChannels ,addUserChannel ,deleteUserChannel} from "@/api/channel";
import { mapState } from "vuex";
import { setItem } from "@/utils/user";
export default {
  name: "ChannelEdit",
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fiexdChannels: [0],
    };
  },
  created() {
    this.loadAllChannels();
  },

  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.allChannels = data.data.channels;
      } catch (err) {
        this.$toast("数据加载失败", err);
      }
    },
    async onAddChannel(channel) {
      this.myChannels.push(channel);
      if (this.user) {
        try{
          await addUserChannel({
          id:channel.id, //频道id
          seq:this.myChannels.length //序号
        })
        } catch (err){
         this.$store('保存失败 请稍后重试')
        }
        //已登录，把数据存储到线上
      
      } else {
        //未登录，把数据存储到本地
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
    onMyChannelClick(item, index) {
      if (this.isEdit) {
        if (this.fiexdChannels.includes(item.id)) {
          return;
        }
        //编辑状态
        if (index <= this.active) {
          this.$emit("update-active", this.active - 1, true);
        }
        this.myChannels.splice(index, 1);
        //删除存储都本地
        this.deleteChannel()
      } else {
        //非编辑状态，执行切换频道
        this.$emit("update-active", index, false);
      }
    },
    async deleteChannel(){
      try {
         if(this.user){
          //登录状态
         await deleteUserChannel(channel.id)
         } else {
          //为登录状态
           setItem("TOUTIAO_CHANNELS", this.myChannels);
         }
      } catch(err){
         this.$toast('操作失败 请稍后')
      }
        
    }
  },
  computed: {
    ...mapState(["user"]),
    recommendChannels() {
      const channels = [];
      this.allChannels.forEach((channel) => {
        const ret = this.myChannels.find((myChannel) => {
          return myChannel.id === channel.id;
        });
        if (!ret) {
          channels.push(channel);
        }
      });
      return channels;
    },
  },
};
</script>
  
  <style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;

      .van-grid-item__text,
      .text {
        font-size: 22px;
        color: #222;
        margin-top: 0px;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recomment-grid {
    .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__content {
        flex-direction: row;

        .van-icon-plus {
          font-size: 22px;
          line-height: 22px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>